<?php
/**
 * Created by PhpStorm.
 * User: 海峡
 * Date: 2021/1/22
 * Time: 9:56
 */
namespace App\Services;

use App\Models\Admin\Goods;
use App\Models\Admin\GoodsSpec;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GoodsService extends BaseService
{
    protected $status = ['goods_status'=>1,'goods_verify'=>1];

    public function add()
   {
        $goods_model = new Goods();
        $data = [
            'goods_name'            => request()->goods_name,                         // 商品名
            'goods_subname'         => request()->goods_subname??'',                  // 副标题
            'goods_no'              => request()->goods_no??'',                       // 商品编号
            'brand_id'              => request()->brand_id,                           // 商品品牌
            'class_id'              => request()->classInfo[2]['id']??0,              // 商品分类
            'goods_master_image'    => request()->goods_master_image??'/uploads/goods_class_thumb/VnwHqvDpVSetOFmpUHd9IFfMOu7y6sZgHLW0yX4B.jpg',                 // 商品主图
            'goods_price'           => abs(request()->goods_price??0),                // 商品价格
            'goods_market_price'    => abs(request()->goods_market_price??0),         // 商品市场价
            'goods_weight'          => abs(request()->goods_weight??0),               // 商品重量
            'goods_stock'           => abs(request()->goods_stock??0),                // 商品库存
            'goods_content'         => request()->goods_content??'',                  // 商品内容详情
            'goods_content_mobile'  => request()->goods_content_mobile??'',           // 商品内容详情（手机）
            'goods_status'          => abs(request()->goods_status)??0,               // 商品上架状态
            'freight_id'            => abs(request()->freight_id)??0,                 // 运费模版ID
            'goods_images'          => request()->goods_images??[],
        ];

//       dd($data);
        try{
            DB::beginTransaction();
            $goods_model = $goods_model->create($data);

            if(isset(request()->goods_attr) && !empty(request()->goods_attr)){
                //前端不太会，手动定义数据
                $skuData = [];
                $specData = GoodsSpec::whereIn('attr_id',explode(',',request()->goods_attr))->get()->toArray();
                foreach ($specData as $k=>$v){
                    $skuData[$k]['goods_image'] = $v['goods_image']??''; // sku图片
                    $skuData[$k]['spec_id'] = $v['id'].','.$v['id']; // sku 属性
                    $skuData[$k]['sku_name'] = $v['name'].','.$v['name']; // sku名称
                    $skuData[$k]['goods_price'] = abs(rand(1,100)??0); // sku价格
                    $skuData[$k]['goods_market_price'] = abs(rand(1,100)??0); // sku市场价
                    $skuData[$k]['goods_stock'] = abs(rand(1,1000)??0); // sku库存
                    $skuData[$k]['goods_weight'] = abs(rand(1,1000)??0); // sku 重量
                }
                $goods_model->goods_skus()->createMany($skuData);
            }
            DB::commit();
            return ['status'=>200,'msg'=>'成功'];
        }catch (\Exception $e){
            DB::rollBack();
            Log::channel('log')->debug('商品添加失败');
            Log::channel('log')->debug($e->getMessage());
            return ['status'=>-1,'msg'=>'失败'];
        }
   }
}